package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.BomInfoListAdapter;
import com.glory.brepda.adapter.ParaInfoListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.BomInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BomInfoQueryActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBom)
    TextView tvBom;
    @BindView(R.id.tvParam)
    TextView tvParam;
    @BindView(R.id.rvBomList)
    RecyclerView rvBomList;
    @BindView(R.id.rvParaList)
    RecyclerView rvParaList;


    private List<BomInfoEntity.ResultBean> bomInfoList = new ArrayList<>();
    private List<BomInfoEntity.RECIPEPARALISTResultBean> paraInfoList = new ArrayList<>();
    private BomInfoListAdapter myAdapter;
    private ParaInfoListAdapter paraAdapter;
    private LinearLayout bomtitle;
    private LinearLayout paratitle;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bom_info_query;
    }



    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("BOM信息查询");
        tvBaseSubmit.setText("查询");
        bomtitle=findViewById(R.id.bomtitle);
        paratitle=findViewById(R.id.paratitle);
        initRecyclerView(rvBomList,this);
        initRecyclerView(rvParaList,this);
        myAdapter = new BomInfoListAdapter(null,false);
        myAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        myAdapter.isFirstOnly(true);
        rvBomList.setAdapter(myAdapter);

        paraAdapter = new ParaInfoListAdapter(null,false);
        paraAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        paraAdapter.isFirstOnly(true);
        rvParaList.setAdapter(paraAdapter);
    }

    @Override
    protected void initData() {
        initEdit(etLotId, this::submit);
    }

    private void submit(String etLotId) {
        FreeUI_EntityCallBack<BaseResponseBean<BomInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<BomInfoEntity>>
                (new TypeToken<BaseResponseBean<BomInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BomInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    BomInfoEntity body = response.body().getResponse().getBody();
                    paratitle.setVisibility(View.VISIBLE);
                    for (BomInfoEntity.ResultBean entity : body.getBOMINFOLIST()){
                        bomInfoList.add(entity);
                    }
                    if(body.getRECIPEPARALIST()!=null){
                        for (BomInfoEntity.RECIPEPARALISTResultBean entity : body.getRECIPEPARALIST()){
                            paraInfoList.add(entity);
                        }
                    }
//                    listSort(bomInfoList);
                    myAdapter.setNewData(bomInfoList);
                    paraAdapter.setNewData(paraInfoList);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BomInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "BOMINFOQUERY");
            }
        };

        bomInfoList.clear();
        paraInfoList.clear();
        JsonObject map = new JsonObject();
        map.addProperty("LOTID",etLotId);
        NetUtils.requestPostNet("IF.BREBOMINFOQUERY","BREBOMRECIPEPARAINFOQUERY", BomInfoQueryActivity.this,map,callBack);
    }

    @OnClick({R.id.tvBom, R.id.tvParam})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBom:
                bomtitle.setVisibility(View.VISIBLE);
                paratitle.setVisibility(View.GONE);
                rvBomList.setVisibility(View.VISIBLE);
                rvParaList.setVisibility(View.GONE);
                setViewStates(true);
                break;
            case R.id.tvParam:
                bomtitle.setVisibility(View.GONE);
                paratitle.setVisibility(View.VISIBLE);
                rvBomList.setVisibility(View.GONE);
                rvParaList.setVisibility(View.VISIBLE);
                setViewStates(false);
                break;
        }
    }
    private void setViewStates(boolean states) {
        tvBom.setBackgroundColor(states ? getResources().getColor(R.color.free_ui_blue_sky_theme_color) : getResources().getColor(R.color.white));
        tvBom.setTextColor(states ? getResources().getColor(R.color.white) : getResources().getColor(R.color.black));
        tvParam.setBackgroundColor(states ? getResources().getColor(R.color.white) : getResources().getColor(R.color.free_ui_blue_sky_theme_color) );
        tvParam.setTextColor(states ? getResources().getColor(R.color.black) : getResources().getColor(R.color.white));

    }
}
